package cn.iocoder.yudao.module.game.controller.admin.bankCard;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.game.controller.admin.bankCard.vo.*;
import cn.iocoder.yudao.module.game.dal.dataobject.bankCard.BankCardDO;
import cn.iocoder.yudao.module.game.service.bankCard.BankCardService;

@Tag(name = "管理后台 - 银行卡")
@RestController
@RequestMapping("/game/bank-card")
@Validated
public class BankCardController {

    @Resource
    private BankCardService bankCardService;

    @PostMapping("/create")
    @Operation(summary = "创建银行卡")
    @PreAuthorize("@ss.hasPermission('game:bank-card:create')")
    public CommonResult<Long> createBankCard(@Valid @RequestBody BankCardSaveReqVO createReqVO) {
        return success(bankCardService.createBankCard(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新银行卡")
    @PreAuthorize("@ss.hasPermission('game:bank-card:update')")
    public CommonResult<Boolean> updateBankCard(@Valid @RequestBody BankCardSaveReqVO updateReqVO) {
        bankCardService.updateBankCard(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除银行卡")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('game:bank-card:delete')")
    public CommonResult<Boolean> deleteBankCard(@RequestParam("id") Long id) {
        bankCardService.deleteBankCard(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得银行卡")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('game:bank-card:query')")
    public CommonResult<BankCardRespVO> getBankCard(@RequestParam("id") Long id) {
        BankCardDO bankCard = bankCardService.getBankCard(id);
        return success(BeanUtils.toBean(bankCard, BankCardRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得银行卡分页")
    @PreAuthorize("@ss.hasPermission('game:bank-card:query')")
    public CommonResult<PageResult<BankCardRespVO>> getBankCardPage(@Valid BankCardPageReqVO pageReqVO) {
        PageResult<BankCardDO> pageResult = bankCardService.getBankCardPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BankCardRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出银行卡 Excel")
    @PreAuthorize("@ss.hasPermission('game:bank-card:export')")
    @OperateLog(type = EXPORT)
    public void exportBankCardExcel(@Valid BankCardPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BankCardDO> list = bankCardService.getBankCardPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "银行卡.xls", "数据", BankCardRespVO.class,
                        BeanUtils.toBean(list, BankCardRespVO.class));
    }

}